require File.join(File.dirname(`node --print "require.resolve('expo/package.json')"`), "scripts/autolinking")
# frozen_string_literal: true

# Important: Artsy must come first, see here: https://github.com/artsy/eigen/issues/12584
source 'https://github.com/artsy/Specs.git'
source 'https://cdn.cocoapods.org/'


require 'dotenv'
Dotenv.load('../.env.shared')

# Resolve react_native_pods.rb with node to allow for hoisting
require Pod::Executable.execute_command('node', ['-p',
  'require.resolve(
    "react-native/scripts/react_native_pods.rb",
    {paths: [process.argv[1]]},
  )', __dir__]).strip

require 'json'
podfile_properties = JSON.parse(File.read(File.join(__dir__, 'Podfile.properties.json'))) rescue {}

project 'Artsy.xcodeproj'

# TODO: Look into these further because they can make our build times faster
# ENV['RCT_USE_RN_DEP'] = '1' if podfile_properties['ios.buildReactNativeFromSource'] != 'true' && podfile_properties['newArchEnabled'] != 'false'
# ENV['RCT_USE_PREBUILT_RNCORE'] = '1' if podfile_properties['ios.buildReactNativeFromSource'] != 'true' && podfile_properties['newArchEnabled'] != 'false'

platform :ios, '16.6'
inhibit_all_warnings! # ignore all warnings from all pods

prepare_react_native_project!


plugin 'cocoapods-patch'

linkage = ENV['USE_FRAMEWORKS']
if linkage != nil
  Pod::UI.puts "Configuring Pod with #{linkage}ally linked Frameworks".green
  use_frameworks! :linkage => linkage.to_sym
end

def check_for_existing_netrc_file
  mapbox_token = ENV['MAPBOX_DOWNLOAD_TOKEN']
  if !mapbox_token || mapbox_token.length <= 1
    raise "You need a MAPBOX_DOWNLOAD_TOKEN in your .env.shared file.\nIf you work at artsy, check 1password.\nOtherwise create your own in the mapbox dashboard. https://docs.mapbox.com/ios/maps/guides/install"
  end
  # mapbox needs credentials in `~/.netrc`, so we put them there and then remove them in post-pod-install.rb
  $netrc_path = File.expand_path('~/.netrc')
  user_already_had_a_netrc_file = File.exists?($netrc_path)
  if user_already_had_a_netrc_file
    system("touch .i-had-a-netrc-file")
  else
    system("rm -rf .i-had-a-netrc-file")
  end
end

def add_mapbox_creds
  File.open($netrc_path, 'a+', 0600) { |f|
    f.write("""machine api.mapbox.com
login mapbox
password #{ENV['MAPBOX_DOWNLOAD_TOKEN']}
""")
  }
end

check_for_existing_netrc_file
add_mapbox_creds

pre_install do |installer|
  $RNMapboxMaps.pre_install(installer)
end


target 'Artsy' do

  use_expo_modules!

  if ENV['EXPO_USE_COMMUNITY_AUTOLINKING'] == '1'
    config_command = ['node', '-e', "process.argv=['', '', 'config'];require('@react-native-community/cli').run()"];
  else
    config_command = [
      'node',
      '--no-warnings',
      '--eval',
      'require(require.resolve(\'expo-modules-autolinking\', { paths: [require.resolve(\'expo/package.json\')] }))(process.argv.slice(1))',
      'react-native-config',
      '--json',
      '--platform',
      'ios'
    ]
  end

  config = use_native_modules!(config_command)

  use_react_native!(
    :path => config[:reactNativePath],
    :hermes_enabled => podfile_properties['expo.jsEngine'] == nil || podfile_properties['expo.jsEngine'] == 'hermes',
    :production => ENV['CIRCLE_BUILD_NUM'],
    # An absolute path to your application root.
    :app_path => "#{Pod::Config.instance.installation_root}/..",
    new_arch_enabled: false
  )

  # Networking
  pod 'AFNetworking', '2.7.0', subspecs: %w[NSURLSession NSURLConnection]
  pod 'AFNetworkActivityLogger'
  pod 'SDWebImage', '5.19.1'
  pod 'INTUAnimationEngine'

  # Core
  pod 'CocoaLumberjack', '3.9.0'
  pod 'FLKAutoLayout', git: 'https://github.com/artsy/FLKAutoLayout.git', branch: 'v1'
  pod 'FXBlurView', '1.6.4'
  pod 'ISO8601DateFormatter', git: 'https://github.com/artsy/iso-8601-date-formatter'
  pod 'Mantle', '1.5.6'
  pod 'MMMarkdown', '0.4'
  pod 'UICKeyChainStore'
  pod 'MARKRangeSlider'

  # Core owned by Artsy
  pod 'ORStackView', '2.0.3'
  pod 'UIView+BooleanAnimations'
  pod 'Aerodramus', '2.0.0'

  # Language Enhancements
  pod 'MultiDelegate'
  pod 'ObjectiveSugar'

  # Workaround for firebase flipper issues
  # See https://github.com/invertase/react-native-firebase/issues/6425#issuecomment-1952403075
  pod 'FirebaseCore', :modular_headers => true
  pod 'FirebaseCoreExtension', :modular_headers => true
  pod 'FirebaseInstallations', :modular_headers => true
  pod 'GoogleDataTransport', :modular_headers => true
  pod 'GoogleUtilities', :modular_headers => true
  pod 'nanopb', :modular_headers => true

  # Swift pods 🎉
  pod 'Then', '2.3.0'
  pod 'Interstellar/Core', git: 'https://github.com/artsy/Interstellar.git', branch: 'observable-unsubscribe'
  pod 'Starscream', '4.0.8'
  # waiting on https://github.com/SwiftyJSON/SwiftyJSON/issues/1154
  pod 'SwiftyJSON', :git => 'https://github.com/SwiftyJSON/SwiftyJSON/', :commit => 'af76cf3ef710b6ca5f8c05f3a31307d44a3c5828'

  # Used in Live Auctions to hold user-state
  pod 'JWTDecode', '2.0.0'


  # Need static libs to fix expo compilation in app delegate
  pod 'BrazeKit', :podspec => 'https://raw.githubusercontent.com/braze-inc/braze-swift-sdk-prebuilt-static/13.2.1/BrazeKit.podspec'
  pod 'BrazeUI', :podspec => 'https://raw.githubusercontent.com/braze-inc/braze-swift-sdk-prebuilt-static/13.2.1/BrazeUI.podspec'
  pod 'BrazeLocation', :podspec => 'https://raw.githubusercontent.com/braze-inc/braze-swift-sdk-prebuilt-static/13.2.1/BrazeLocation.podspec'

  target 'ArtsyTests' do
    inherit! :search_paths
    use_frameworks!

    pod 'OHHTTPStubs', '3.1.2'
    pod 'XCTest+OHHTTPStubSuiteCleanUp'
    pod 'Specta', '~> 2.0'
    pod 'Expecta', '1.0.6'
    pod 'Expecta+Snapshots', '3.2.0'
    pod 'OCMock'
    pod 'Forgeries/Mocks'

    # Swift pods 🎉
    pod 'Quick', '2.0.0'
    pod 'Nimble', '~> 13.0'
    pod 'Nimble-Snapshots', '9.8.0'
  end

  post_install do |installer|
    react_native_post_install(
      installer,
      config[:reactNativePath],
      :mac_catalyst_enabled => false,
      # :ccache_enabled => true
    )

    $RNMapboxMaps.post_install(installer)

    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['EXPANDED_CODE_SIGN_IDENTITY'] = ""
            config.build_settings['CODE_SIGNING_REQUIRED'] = "NO"
            config.build_settings['CODE_SIGNING_ALLOWED'] = "NO"
            config.build_settings["IPHONEOS_DEPLOYMENT_TARGET"] = "16.6"
        end
    end
  end
end
